<?php

/// My comments are full of lulz for the people who know english :)

require_once($_SERVER["DOCUMENT_ROOT"]."/app/db/iDatabase.php");
require_once($_SERVER["DOCUMENT_ROOT"]."/app/logger.php");

class MySQLDatabase implements iDatabase
{
	private $m_dbcontext = NULL;
	
	private $m_hostname;
	private $m_dbname;
	private $m_username;
	private $m_passwd = "";
	
	private $m_encoding = "utf-8";
	
	private $m_result;
	
	private $m_readyflag = null;
	private $m_rows;
		
	public function __construct($hostname = "", $dbname = "", $username = "", $passwd = "", $encoding = "")
	{
		$flag = true;
		/// The values have to be strongly checked
		if ( is_string($hostname) && strlen($hostname) > 0)
			$this->m_hostname = $hostname;
		else
		{ 
			$flag = false;
			Logger::getInstance()->error("Invalid hostname in MySQLDatabase::__construct() !");
		};
		if ( is_string($dbname) && strlen($dbname) > 0)
			$this->m_dbname = $dbname;
		else 
		{	
			$flag = false;
			Logger::getInstance()->error("Invalid database name in MySQLDatabase::__construct() !");
		};
		if ( is_string($username) && strlen($username) > 0)
			$this->m_username = $username;
		else 
		{
			$flag = false;
			Logger::getInstance()->error("Invalid username in MySQLDatabase::__construct() !");
		};
		/// password could be left blank
		if ( is_string($passwd) )
			$this->m_passwd = $passwd;
		/// encoding has a default initialization
		if ( is_string($encoding) && strlen($encoding) > 0)
			$this->m_encoding = $encoding;

		$this->m_readyflag = $flag;			

		if ($this->m_readyflag)
			$this->connect();
		else
			Logger::getInstance()->error("No call to MySQLDatabase::connect() in MySQLDatabase:__construct() : some params are needed but empty !");
		
	}
	
	private function connect()
	{
		$flag = true;
		$this->m_dbcontext = @mysql_connect($this->m_hostname, $this->m_username, $this->m_passwd);
		if (! $this->m_dbcontext)
		{
			$flag = false;
			Logger::getInstance()->error("Not connected to MySQL database in MySQLDatabase::connect(), returned ".mysql_error($this->m_dbcontext));
		};
		
		$this->m_readyflag = $flag;
		
		if ($this->m_readyflag)
			$this->selectDB();
		else
			Logger::getInstance()->error("No call to MySQLDatabase::selectDB() in MySQLDatabase::connect() : connection was unsuccesful !");
	}
	
	private function selectDB()
	{
		$flag = @mysql_select_db($this->m_dbname);
		if (! $flag )
		{
			Logger::getInstance()->error("Database selecting error in MySQLDatabase::selectDB(), returned ".mysql_error($db));
		};

		if ($this->m_readyflad = $flag)
			$this->setEncoding($this->m_encoding);

		return $this->m_readyflag;			
	}

	public function setEncoding($encoding)
	{
		if ( is_string($encoding) && strlen($encoding) > 0)
		{		
			if ($this->isReady())
			{
				@mysql_query("set character_set_client	='".$encoding."'", $this->m_dbcontext);
				@mysql_query("set character_set_results	='".$encoding."'", $this->m_dbcontext);
			}
		}
		else
			Logger::getInstance()->error("Invalid param received in MySQLDatabase::setEncoding(), encoding is not changed !");
	}
	
	/// for checking connection state
	public function isReady()
	{
		return $this->m_readyflag == true;	
	}
	
	/// returns an array
	public function query($string)
	{
		if ( is_string($string) && strlen($string) > 0)
		{
			$this->m_result = @mysql_query($string);
			if ($this->m_result)
			{
				if (is_bool($this->m_result) )
					return 0;
				if ( ($this->m_rows = mysql_num_rows($this->m_result) ) == 1)
				{
					return mysql_fetch_array($this->m_result);
				};

				while ($row = mysql_fetch_array($this->m_result)) {
					$res[] = $row;
				}
				return $res;
			}
			else
			{
				Logger::getInstance()->error("Query error in MySQLDatabase::query() returned ".mysql_error($this->m_dbcontext));
				return 0;
			}
				
		}
		else
		{
			Logger::getInstance()->error("Invalid parameter accepted in MySQLDatabase::query() !");
			return 0;
		}
	}
	
	public function rowsAffected()
	{
		return $this->m_rows;
	}

}

?>